package com.xiaoyu.lingdian.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.entity.CoreCartOrder;
import com.xiaoyu.lingdian.entity.CoreCartOrderDetail;
import com.xiaoyu.lingdian.service.CoreCartOrderDetailService;
import com.xiaoyu.lingdian.service.CoreCartOrderService;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import com.xiaoyu.lingdian.vo.CoreCartOrderDetailVO;
import com.xiaoyu.lingdian.vo.CoreCartOrderVO;

/**
 *购物车订单
 */
@Controller
@RequestMapping(value="/coreCartOrder")
public class CoreCartOrderController extends BaseController {
	
	@Autowired
	private CoreCartOrderService coreCartOrderService;
	
	@Autowired
	private CoreCartOrderDetailService coreCartOrderDetailService;	
	
	/**
	 *根据订单编号分页查询商品订单列表
	 * 
	 * @param orderUuid
	 * @param crporStatus
	 * @param pageNum
	 * @param pageSize
	 * @param response
	 */
	@RequestMapping(value = "/find/by/cnd", method = RequestMethod.POST)
	public void findByCnd(String orderUuid,String userUuid, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.findByCnd]:begin findByCnd.");
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		Page<CoreCartOrder> page = coreCartOrderService.findCoreCartOrderByCnd(orderUuid, userUuid, pageNum, pageSize);
		Page<CoreCartOrderVO> pageVO = new Page<>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());		
		List<CoreCartOrderVO> list = new ArrayList<>();
		CoreCartOrderVO vo = new CoreCartOrderVO();
		for (CoreCartOrder coreCartOrder : page.getResult()) {
			vo = new CoreCartOrderVO();
			vo.convertPOToVO(coreCartOrder);

			list.add(vo);
		}
		pageVO.setResult(list);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询购物车商品订单成功", pageVO), response);
		logger.info("[CoreCartOrderController.findByCnd]:end findByCnd.");
	}
	
	/**
	 * 查看购物车订单
	 * 
	 * @param crporUuid
	 * @param response
	 */
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCartOrder(String orderUuid, HttpServletResponse response) {
		logger.info("[CoreCartOrderController.viewsCartOrder]:begin viewsCartOrder");
		if (StringUtils.isBlank(orderUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少orderUuid参数！"), response);
			return;
		}
		CoreCartOrder coreCartOrder = new CoreCartOrder();
		coreCartOrder.setCrporUuid(orderUuid);
		coreCartOrder = coreCartOrderService.getCoreCartOrder(coreCartOrder);
		CoreCartOrderVO vo = new CoreCartOrderVO();
		vo.convertPOToVO(coreCartOrder);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查看购物车订单详情成功！", vo), response);
		logger.info("[CoreCartOrderController.viewsCartOrder]:end viewsCartOrder");
	}	
	
	/**
	 * 根据订单编号查询商品明细
	 * 
	 * @param orderUuid
	 * @param pageNum
	 * @param pageSize
	 * @param response
	 */	
	@RequestMapping(value = "/find/by/cndDetail", method = RequestMethod.POST)
	public void findByCndDetail(String orderUuid, Integer pageNum, Integer pageSize, HttpServletResponse response) {
		logger.info("[CorePointsOrderController.findByCndDetail]:begin findByCndDetail.");

		CoreCartOrderDetail coreCartOrderDetail = new CoreCartOrderDetail();
		coreCartOrderDetail.setCrporOrderUuid(orderUuid);
		List<CoreCartOrderDetail> coreCartOrderDetails = coreCartOrderDetailService.findCoreCartOrderDetailsByOrderUuid(coreCartOrderDetail);
		CoreCartOrderDetailVO vo;
		List<CoreCartOrderDetailVO> vos = new ArrayList<CoreCartOrderDetailVO>();
		for(CoreCartOrderDetail coreCartOrderDetailN : coreCartOrderDetails){
			vo = new CoreCartOrderDetailVO();
			vo.setCriglGoodsName(coreCartOrderDetailN.getCriglGoodsName());
			vo.setCriglGoodsAmount(coreCartOrderDetailN.getCriglGoodsAmount());
			vo.setCriglGoodsPrice(coreCartOrderDetailN.getCriglGoodsPrice());
			vo.setCriglGoodsScore(coreCartOrderDetailN.getCriglGoodsScore());
			vo.setCrporOrderUuid(coreCartOrderDetailN.getCrporOrderUuid());
			
			vos.add(vo);
		}
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "查询购物车商品订单商品信息成功",vos), response);
		logger.info("[CoreCartOrderController.findByCndDetail]:end findByCndDetail.");
	}
	
	/**
	 * 修改订单订单（发货、已完成）、备注
	 * 
	 * @param crporUuid
	 * @param crporStatus
	 * @param crporRemarks
	 * @param response
	 */
	@RequestMapping(value="/update/updateCartOrder", method=RequestMethod.POST)
	public void updateCartOrder(String crporUuid, Integer crporStatus, String crporRemarks, HttpServletResponse response) {
		logger.info("[CoreCartOrderController.updateCartOrder]:begin updateCartOrder");
		if (StringUtils.isBlank(crporUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "缺少crporUuid参数！"), response);
			return;
		}
		if (null == crporStatus || 0 == crporStatus) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请输入购物车订单状态！"), response);
			return;
		}
		CoreCartOrder coreCartOrder = new CoreCartOrder();
		coreCartOrder.setCrporUuid(crporUuid);
		coreCartOrder.setCrporStatus(crporStatus);
		coreCartOrder.setCrporRemarks(crporRemarks);
		coreCartOrderService.updateCoreCartOrder(coreCartOrder);
		
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "更新购物车订单成功！"), response);
		logger.info("[CoreCartOrderController.updateCartOrder]:end updateCartOrder");
	}	
	
}